<template>
  <el-select v-if="group" v-model="form" @change="handleChange" v-bind="$attrs">
    <el-option-group v-for="group in options" :key="group.value" :label="group.label" :value="group.value">
      <el-option v-for="item in group.options" :disabled="setDisabled(item.value)" :key="item.value" :label="item.label"
        :value="item.value"></el-option>
    </el-option-group>
  </el-select>
  <el-cascader v-else-if="cascader" v-model="form" :options="options" @change="handleChange" v-bind="$attrs"></el-cascader>
  <el-select v-else v-model="form" @change="handleChange" v-bind="$attrs">
    <el-option :disabled="setDisabled(item.value)" v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
  </el-select>
</template>

<script>
  import { listSensorType } from "@/api/slope/sensorType";
  import { noRoleListSupplier } from "@/api/slope/supplier";
  import { noRoleListPoint } from "@/api/slope/point";
  import { listNvr } from "@/api/slope/nvr";
  import { listSensorConfiguration } from "@/api/page/device.js"
  import vModelMixins from "../../../../utils/mixins/vModelMixins";
  export default {
    name: "selector",
    mixins: [vModelMixins],
    props: {
      type: {
        type: String
      },
      disabledOptions: {
        type: Array | Number | String,
        default: undefined
      },
      group: {
        type: Boolean,
        default: false
      },
      cascader: {
        type: Boolean,
        default: false
      },
      defaultValue: {
        type: String | Number | String,
        default: undefined
      },
      list: {
        type: Array,
        default: () => []
      },
      showAll: {
        type: String,
        default: undefined
      }
    },
    data() {
      return {
		    options: [],
      };
    },
    computed: {
      selectItem() {
        if (this.group) {
          return undefined;
        }
        if (this.cascader) {
          return undefined;
        }
        return this.options.find(i => i.value === this.form);
      }
    },
    watch: {
      list(val) {
        if (this.showAll !== undefined) {
          try {
            this.options = [{
                label: this.showAll,
                value: undefined
              },
              ...val
            ];
          } catch (error) {}
        } else {
          this.options = val;
        }
      }
    },
    methods: {
      setDisabled(value) {
        let disabledOptions = this.disabledOptions;
        if (!disabledOptions) return false;
        if (Array.isArray(disabledOptions))
          return disabledOptions.find(i => i == value);
        return value == disabledOptions;
      },
      handleChange(val) {
        this.$emit("change", { ...this.selectItem
        });
      },
      getItem(val) {
        return this.options.find(i => i.value == val);
      },
      async _initData() {
        let res;
        let data = [];
        const params = {
          data: {}
        };
        switch (this.type) {
          case "otherDeviceType": //设备类型
            res = await this.getDicts("device_type");
            for (var i = 0; i < res.data.length; i++) {
              var obj = res.data[i];
              if (obj.dictLabel != "传感器" && obj.dictLabel != "摄像头") {
                data.push({
                  value: parseInt(obj.dictValue),
                  label: obj.dictLabel
                })
              }
            }
            break;
          case "deviceType": //设备类型
            res = await this.getDicts("device_type");
            for (var i = 0; i < res.data.length; i++) {
              var obj = res.data[i];
              data.push({
                value: parseInt(obj.dictValue),
                label: obj.dictLabel
              })
            }
            break;
          case "sensorType": //传感器类型
            res = await listSensorType();
            data = res.rows.map(i => {
              return {
                value: i.sensorTypeId,
                label: i.typeName
              };
            });
            break;
          case "sensorState": //设备状态
            res = await this.getDicts("sensor_state");
            data = res.data.map(i => {
              return {
                value: i.dictValue,
                label: i.dictLabel
              };
            });
            break;
          case "deviceSupply": //供应商
            res = await noRoleListSupplier();
            for (var i = 0; i < res.rows.length; i++) {
              if (res.rows[i].isValid === '0') {
                var obj = res.rows[i];
                data.push({
                  value: obj.supplierId,
                  label: obj.supplierName
                });
              }
            }
            break;
          case "devicePoint": //点位
            res = await noRoleListPoint();
            for (var i = 0; i < res.rows.length; i++) {
              if (res.rows[i].isValid != 0) {
                var obj = res.rows[i];
                data.push({
                  value: obj.pointId,
                  label: obj.pointName
                });
              }
            }
            break;
          case "nvr": //硬盘录像机
            res = await listNvr();
            data = res.rows.map(i => {
              return {
                value: i.nvrId,
                label: i.nvrName
              };
            });
            break;
          case "cameraType": //摄像头类型
            res = await this.getDicts("camera_type");
            data = res.data.map(i => {
              return {
                value: parseInt(i.dictValue),
                label: i.dictLabel
              };
            });
            break;
          case "alarmLevel": //告警级别
            res = await this.getDicts("alarm_level");
            data = res.data.map(i => {
              return {
                value: parseInt(i.dictValue),
                label: i.dictLabel
              };
            });
            break;
          case "alarmHandleState": //告警处理状态
            res = await this.getDicts("alarm_handle_state");
            data = res.data.map(i => {
              return {
                value: parseInt(i.dictValue),
                label: i.dictLabel
              };
            });
            break;
		      case "sensorList": //传感器列表
            res = await listSensorConfiguration();
            // this.$emit('sensorItem', res.rows);
            data = res.rows.map(i => {
              return {
                value: i.sensorId,
                label: i.device.deviceName
              };
            });
            break;
          default:
            //根据传入的list 渲染selector
            data = this.list;
            break;
        }
        if (this.group) {
          this.options = this.formatOptions(data, "options");
        } else if (this.cascader) {
          this.options = this.formatOptions(data, "children");
        } else {
          this.options = data;
        }
        if (this.showAll !== undefined) {
          try {
            this.options.unshift({
              label: this.showAll,
              value: undefined
            });
          } catch (error) {}
        }
        if (this.defaultValue) this.form = this.defaultValue;
        // this.handleChange();
      },
      formatOptions(arr, key) {
        return arr.map(i => {
          let ret = { ...i
          };
          if (i.childrenList) {
            ret[key] = this.formatOptions(i.childrenList);
          }
          return ret;
        });
      }
    },
    created() {
      this._initData();
    }
  };
</script>
